<?php
session_save_path("/tmp");
session_start();
/*
 *      guess.php
 *
 *      Copyright 2010 Nolan Baker <hendersonhasselbalch@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Text Zendo</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Geany 0.18" />
</head>

<body>
<?php

########################################################################
# IMPORTS AND CONNECTIONS AND SUCH
########################################################################
include("func.php");
$con = mysql_connect("localhost", "nbaker", "");
mysql_query("use zendo;", $con);


########################################################################
# INITIALIZE THE GUESS LIST
########################################################################
if ($_POST){
    if ($_SESSION['stones'] <= 0){
        header('Location: zendo.php');
    }
    if (isset($_POST['type'])){
        if ($_POST['type'] == 'guess' and
            $_SESSION['new_words'] and
            $_SESSION['stones'] > 0){
                
            # take a stone for guessing if coming from zendo.php
            $_SESSION['stones'] = $_SESSION['stones'] - 1;

            $_SESSION['new_words'] = 0;

            # then create an array of buddha and non-buddha words
            $_SESSION['guess_words'] = array();
            for ($i = 0; $i < 4; $i++) {
                $b = randWord($_SESSION['rule'], 1);
                $n = randWord($_SESSION['rule'], 0);
                $r = randWord($_SESSION['rule'], rand(0,1));
                
                array_push($_SESSION['guess_words'], $b, $n, $r);
            }
            shuffle($_SESSION['guess_words']);
        }
        if ($_POST['type'] == 'check') {
            $examples = array();
            $correct = true;
            for($i = 0; $i < sizeof($_SESSION['guess_words']); $i++){
                $word = $_SESSION['guess_words'][$i];
                $guess = $_POST[$word];
                $nature = isBuddha($word, $_SESSION['rule']);
                print $guess;
                print $nature.'<br>';
                if ($guess != $nature) {
                    array_push($examples, $word);
                    $correct = false;
                }
            }
            if ($correct) {
                $sql = "INSERT INTO user_descriptions (rule_id, game_id, description, correct, moves) VALUES ('"
                        .$_SESSION['rule']."',".$_SESSION['game'].",'"
                        .$_POST['rule']."',1,".$_SESSION['num_moves'].");";
                mysql_query($sql, $con);
                header('Location: win.php');
            }
            else {
                $sql = "INSERT INTO user_descriptions (rule_id, game_id, description, correct, moves) VALUES ('"
                        .$_SESSION['rule']."',".$_SESSION['game'].",'"
                        .$_POST['rule']."',0,".$_SESSION['num_moves'].");";
                mysql_query($sql, $con);
                
                shuffle($examples);
                $word = $examples[0];
                $nature = isBuddha($word, $_SESSION['rule']);
                addWord($word, $nature);
                $_SESSION['last_word'] = $word;
                $_SESSION['new_words'] = 1;
                unset($_SESSION['guess_words']);
                header('Location: zendo.php');
            }
        }
    }
    # give error feedback

    # redirect either to win.php or back to zendo.php
}

########################################################################
# LIST THE WORDS
########################################################################
print "Good job. You've made some serious progress.</p>
       Correctly identify each of the following words as buddha or
       not buddha then state your rule and you win!";
print "<table border=0 cellpadding=10>
     <tr><td> </td><td>BUDDHA</td><td>NOT</td></tr>
     <form action='guess.php' method='post'>";

for ($i = 0; $i < sizeof($_SESSION['guess_words']); $i++){
    print "<tr><td>".$_SESSION['guess_words'][$i]."</td><td>
         <input type='radio' name='".$_SESSION['guess_words'][$i]."' value='1'></td><td>
         <input type='radio' name='".$_SESSION['guess_words'][$i]."' value='0'></td></tr>";
}
print "</table>";
print "Type your rule here: <input type='text' name='rule'>
      <input type='submit' value='guess'>
      <input type='hidden' name='type' value='check'>
      </form>";
?>
</body>
</html>
